package com.gxa.learning.pc.service.impl;


import com.gxa.learning.core.common.ExceptionCode;
import com.gxa.learning.core.exception.BusinessException;
import com.gxa.learning.core.pojo.ExamineApproval;
import com.gxa.learning.core.pojo.FlowPathNode;
import com.gxa.learning.core.pojo.TrainPlan;
import com.gxa.learning.core.pojo.TrainPlanVo;
import com.gxa.learning.core.service.impl.BaseServiceImpl;
import com.gxa.learning.core.util.StringUtil;
import com.gxa.learning.pc.mapper.TrainPlanMapper;
import com.gxa.learning.pc.service.TrainPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 培训计划的业务层实现类
 *
 * @author YXQ
 * @date 2021/12/2 18:20
 */
@Service("trainPlanService")
@Transactional(rollbackFor = Throwable.class)
public class TrainPlanServiceImpl extends BaseServiceImpl<TrainPlan> implements TrainPlanService {
    @Autowired
    TrainPlanMapper trainPlanMapper;

    private static final Integer ZERO = 0;

    @Override
    public List<TrainPlanVo> selectQueryAndPage(String name, String planType, String type, Integer page, Integer size) {
        // 对接收到的参数作处理然后放入Map中，方便传参
        Map<String, Object> map = new HashMap<>(3);
        // 通过StringUtil工具类判断
        // 计划的标题
        if (!StringUtil.isEmpty(name)) {
            map.put("title", name);
        } else {
            map.put("title", null);
        }
        // 计划的审批状态
        if (!StringUtil.isEmpty(planType)) {
            map.put("planType", planType);
        } else {
            map.put("planType", null);
        }
        // 计划的类型
        if (!StringUtil.isEmpty(type)) {
            map.put("type", type);
        } else {
            map.put("type", null);
        }
        // 这是一个一对多关系查询的分页，通过分页插件，分页的方式。不能满足了，只能手动分页(物理分页)
        // 分页插件的问题、
        // 1.开启分页必须放在查询的操作之前,在这个例子中，分页必须在36这行代码之前
        // 2.分页插件只是简单根据查询到的结果集进行分页，如果这个查询结果具有一对多关系之类的，
        //      并不能满足查询的需求，它并不能通过按一对多关系注入之后，再分页不过将集合传入 PageInfo.of(trainPlanVos)
        //      还是能得到总记录数，数据的list集合，总页数等信息。即44到47行
        // 3.如果分页的条件是默认的第一页，最后出现的sql语句中limit只会带有size
        return trainPlanMapper.selectQueryAndPage(map);
    }

    @Override
    public TrainPlanVo selectTrainPlanDetails(Long id) {
        return trainPlanMapper.selectTrainPlanDetails(id);
    }

    @Override
    public List<TrainPlanVo> selectWaitExamineList(Long id) {
        return trainPlanMapper.selectWaitExamineList(id);
    }

    @Override
    public List<TrainPlanVo> selectAlreadyExamineList(Long id) {
        return trainPlanMapper.selectAlreadyExamineList(id);
    }

    @Override
    public List<TrainPlanVo> selectGetMyExamineApprovalsList(Long id) {
        return trainPlanMapper.selectGetMyExamineApprovalsList(id);
    }

    @Override
    public Boolean agreeOrRefuse(Long id, Long planId, String option) throws BusinessException {


        // 得到当前审核计划的意见征询节点,以及征询的总的意见数
        //
        return null;
    }

    @Override
    public FlowPathNode selectByUserAndPlan(Long userId, Long planId) {
        return trainPlanMapper.selectByUserAndPlan(userId, planId);
    }

    @Override
    public ExamineApproval selectOpinionCount(Long planId) {
        return trainPlanMapper.selectOpinionCount(planId);
    }

    @Override
    public Integer selectAlreadyAgreeCount(Long planId) throws BusinessException {
        Map<String, Object> map = trainPlanMapper.selectAlreadyAgreeCount(planId);
        Object o = map.get("count");
        if (o == null) {
            throw new BusinessException(ExceptionCode.ARGUMENTS_ERROR);
        }
        return Integer.parseInt(o.toString());
    }

    @Override
    public Integer selectAlreadyNotAgreeCount(Long planId) throws BusinessException {
        Map<String, Object> map = trainPlanMapper.selectAlreadyNotAgreeCount(planId);
        Object o = map.get("count");
        if (o == null) {
            throw new BusinessException(ExceptionCode.ARGUMENTS_ERROR);
        }
        return Integer.parseInt(o.toString());
    }

    @Override
    public Integer selectExamineApprovalCount(Long planId) throws BusinessException {
        Map<String, Object> map = trainPlanMapper.selectExamineApprovalCount(planId);
        Object o = map.get("count");
        if (o == null) {
            throw new BusinessException(ExceptionCode.ARGUMENTS_ERROR);
        }
        return Integer.parseInt(o.toString());
    }


}
